<?php
include_once 'IUserRepository.php';
include_once 'User.php';
include_once 'BaseSqlRepository.php';
class SqlUserRepository extends BaseSqlRepository implements IUserRepository {
	private $url;
	private $port;
	private $username;
	private $password;
	public function __construct($databseUrl, $port, $username, $password) {
		$this->password = $password;
		$this->port = $port;
		$this->url = $databseUrl;
		$this->username = $username;
	}
	public function addUser(User $user) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "INSERT INTO UserT(Username, PasswordHash, privileges, email, Name) VALUES (?,?,?,?,?)" );
		$stmt->bind_param ( "ssiss", $user->getUserName (), $user->getPassword (), $user->getPrivileges (), $user->getEmail (), $user->getName () );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
	public function getUser($userName) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		
		$stmt = $mysqli->prepare ( "SELECT  Username, PasswordHash, privileges, email, Name FROM  UserT WHERE UserName =  ?" );
		if (! $stmt) {
			return NULL;
		}
		$stmt->bind_param ( "s", $userName );
		
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		
		/* bind variables to prepared statement */
		$stmt->bind_result ( $username, $pass, $privileges, $email, $name );
		
		$user = NULL;
		
		/* fetch values */
		while ( $stmt->fetch () ) {
			
			$user = new User ( $username );
			$user->setEmail ( $email );
			$user->setName ( $name );
			$user->setPrivileges ( $privileges );
			$user->setPassword ( $pass );
		}
		
		$stmt->close ();
		mysqli_close ( $mysqli );
		
		return $user;
	}
	public function getAllUsers() {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$query = 'SELECT * FROM UserT ORDER BY UserName';
		
		$result = $mysqli->query ( $query );
		if (! $result) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		$users = array ();
		
		while ( $row = $result->fetch_assoc () ) {
			$user = new User ( $row ["Username"] );
			$user->setEmail ( $row ["email"] );
			$user->setName ( $row ["Name"] );
			$user->setPrivileges ( $row ["privileges"] );
			$user->setPassword ( $row ["PasswordHash"] );
			
			$users [] = $user;
		}
		
		$result->free ();
		mysqli_close ( $mysqli );
		return $users;
	}
	public function removeUser($userName) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "DELETE FROM UserT WHERE Username = ?" );
		if (! $stmt) {
			return false;
		}
		$stmt->bind_param ( "s", $userName );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
	public function checkUser($userName, $password) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		
		$stmt = $mysqli->prepare ( "SELECT Username, PasswordHash, privileges, email, Name FROM  UserT WHERE Username =  ? AND PasswordHash = ?" );
		if (! $stmt) {
			return NULL;
		}
		$stmt->bind_param ( "ss", $userName, $password );
		
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		
		/* bind variables to prepared statement */
		$stmt->bind_result ( $username, $pass, $privileges, $email, $name );
		
		$user = NULL;
		
		/* fetch values */
		while ( $stmt->fetch () ) {
			
			$user = new User ( $username );
			$user->setEmail ( $email );
			$user->setName ( $name );
			$user->setPrivileges ( $privileges );
			$user->setPassword ( $pass );
		}
		
		$stmt->close ();
		mysqli_close ( $mysqli );
		
		return $user;
	}
	public function updateUser(User $user) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "UPDATE UserT SET PasswordHash = ?, email = ?, name = ?, privileges = ? WHERE Username = ?" );
		if (! $stmt) {
			return false;
		}
		$stmt->bind_param ( "sssis", $user->getPassword (), $user->getEmail (), $user->getName (), $user->getPrivileges (), $user->getUserName () );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
}
